﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Rizmar.RizmarDataSetTableAdapters;

namespace Rizmar
{
    public partial class EscolhaDescontos : Form
    {
        Form parent;
        private decimal totalDesconto = 0;

        public EscolhaDescontos()
        {
            InitializeComponent();
        }
        public EscolhaDescontos(LancamentoForm form)
        {
            parent = form;
            InitializeComponent();
        }

        private void EscolhaDescontos_Load(object sender, EventArgs e)
        {
            okButton.Focus();
            descontoTableAdapter.Fill(rizmarDataSet.Desconto, Convert.ToInt32((parent as LancamentoForm).textBoxCódigo.Text));
        }

        private void okButton_Click(object sender, EventArgs e)
        {          
            decimal somaDesconto = 0;
            DescontoTableAdapter dta = new DescontoTableAdapter();
            RizmarDataSet.DescontoDataTable ddt = new RizmarDataSet.DescontoDataTable();
                        
            int i = 0;
            for (i = 0; i < dataGridViewDesconto.Rows.Count; i++)
            {
                if (Convert.ToBoolean(dataGridViewDesconto.Rows[i].Cells[0].Value) == false)
                    dta.DeleteDescontoEspecifico(Convert.ToInt32((parent as LancamentoForm).textBoxCódigo.Text), Convert.ToInt32(dataGridViewDesconto.Rows[i].Cells[3].Value));
            }
            somaDesconto = calculaTotalDesconto();
            setTotalDesconto(somaDesconto);
            DialogResult = DialogResult.OK;
            this.Close();
        }

        public decimal calculaTotalDesconto()
        {
            decimal soma = 0;
            int i = 0;
            for (i = 0; i < dataGridViewDesconto.Rows.Count; i++)            
                soma += Convert.ToDecimal(dataGridViewDesconto.Rows[i].Cells[1].Value);                    
            
            return soma;
        }

        public decimal getTotalDesconto() 
        {
            return totalDesconto;
        }

        public void setTotalDesconto(decimal desc)
        {
            totalDesconto = desc;
        }

        private void cancelarButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
